<?php

  // BEGIN - CAPTCHA Encryption Functionality
  $captcha_encryption_key = 'BuyX0nl1n3 C4pTch4 p4ssw0rd';

  function CAP_RC4($key, $data) {
    // To Encrypt - Call CAP_RC4 with key and plain text.
    // To Decrypt - Call CAP_RC4 with key and encrypted text.
    $key_length = strlen($key);
    for ($i = 0; $i < 255; $i++) {
      $key[$i] = ord(substr($key, ($i % $key_length)+1, 1));
      $counter[$i] = $i;
    }
    for ($i = 0; $i < 255; $i++) {
      $x = ($x + $counter[$i] + $key[$i]) % 256;
      $temp_swap = $counter[$i];
      $counter[$i] = $counter[$x];
      $counter[$x] = $temp_swap;
    }
    for ($i = 0; $i < strlen($data); $i++) {
      $a = ($a + 1) % 256;
      $j = ($j + $counter[$a]) % 256;
      $temp = $counter[$a];
      $counter[$a] = $counter[$j];
      $counter[$j] = $temp;
      $k = $counter[(($counter[$a] + $counter[$j]) % 256)];
      $Xcipher = ord(substr($data, $i, 1)) ^ $k;
      $Xcrypt .= chr($Xcipher);
    }
    return $Xcrypt;
  }

  function urlsafe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
  }

  function urlsafe_b64decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return base64_decode($data);
  }

  // END - CAPTCHA Encryption Functionality

  $code = CAP_RC4($captcha_encryption_key, urlsafe_b64decode($HTTP_GET_VARS['code']));
  
  for ($i = 1; $i <= 6; $i++) {
     $count = $i - 1;
     $text[$i] = $code{$count};
  }

  // create image
  $im = @imagecreate(70,25);
  $backgroundColor = imagecolorallocate($im,150,150,150);
  $textColor = imagecolorallocate($im,0,0,0);
  $lineColor = imagecolorallocate($im,100,100,100);
  imageline($im,0,0,23,25,$lineColor);
  imageline($im,23,0,46,25,$lineColor);
  imageline($im,46,0,69,25,$lineColor);
  imageline($im,0,25,70,0,$lineColor);
  imagestring($im, rand(4,5), 4, 2, $text[1], $textColor);
  imagestring($im, rand(4,5), 15, 8, $text[2], $textColor);
  imagestring($im, rand(4,5), 26, 5, $text[3], $textColor);
  imagestring($im, rand(4,5), 37, 8, $text[4], $textColor);
  imagestring($im, rand(4,5), 47, 1, $text[5], $textColor);
  imagestring($im, rand(4,5), 58, 6, $text[6], $textColor);
  imagepng($im);
  imagedestroy($im);
?>  
